import axios from "axios";
interface ChatMessage {
  role: "user" | "assistant" | "system";
  content: string;
}
  
const model = "gpt-3.5-turbo";

axios.defaults.headers.post["Content-Type"] = "application/json";

export async function chat(msg:string, apiKey: string) {
  try {
    const result = await fetch("https://api.openai.com/v1/chat/completions", {
      method: "post",
      headers: {
        "Content-Type": "application/json",
        Authorization: `Bearer ${apiKey}`,
      },
      body: JSON.stringify({
        model,
        stream: true,
        messages:[{role:"user","content":msg}]
      }),
    });

    return {
      status: "success",
      data: result.body,
    };
  } catch (error: any) {
    console.log(error);
    return {
      status: "error",
      message: error,
    };
  }
}
